package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.admin.domain.AdUser;
import com.zhentao.admin.pojo.AdUserPageDto;
import com.zhentao.admin.pojo.UserDto;
import com.zhentao.common.AppJwtUtil;
import com.zhentao.common.BCrypt;
import com.zhentao.common.dtos.PageResponseResult;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.common.enums.AppHttpCodeEnum;
import com.zhentao.service.AdUserService;
import com.zhentao.mapper.AdUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
* @author 白耀东
* @description 针对表【ad_user(用户表)】的数据库操作Service实现
* @createDate 2024-08-30 16:50:46
*/
@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser>
    implements AdUserService{

    @Resource
    private AdUserMapper adUserMapper;

    /**
     * 用户登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(UserDto dto) {

        // 验证参数是否为空
        if (ObjectUtils.isEmpty(dto))
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"账号密码不能为空");
        }
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdUser::getUsername, dto.getUsername());

        AdUser adUser = adUserMapper.selectOne(wrapper);

        // 判断用户是否存在
        if (adUser == null)
        {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }
        // 比较
        if (BCrypt.checkpw(dto.getPassword(), adUser.getPassword())){
            // 返回jwt
            Map<String, Object> map = new HashMap<>();

            map.put("token", AppJwtUtil.getToken(Long.valueOf(adUser.getId())));
            adUser.setPassword("******");
            map.put("user",adUser);
            return ResponseResult.okResult(map);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
    }

    /**'
     * 后台  查看所有员工
     * @param dto
     * @return
     */
    @Override
    public ResponseResult selectOnManageUser(AdUserPageDto dto) {
        LambdaQueryWrapper<AdUser> wrapper = new LambdaQueryWrapper<>();
        Page<AdUser> page = new Page<>(dto.getOffset(),dto.getPagesize());
        Page<AdUser> adUserPage = adUserMapper.selectPage(page, wrapper);
        return ResponseResult.okResult(adUserPage);
    }

    /**
     * 注册 新的员工
     * @param adUser
     * @return
     */
    @Override
    public ResponseResult regiestManage(AdUser adUser) {
        //
        if (ObjectUtils.isEmpty(adUser)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不能为空");
        }

        // 验证用户名
        AdUser user = adUserMapper.selectOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getUsername, adUser.getUsername()));
        if (user != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"用户名已存在");
        }

        // 进行注册
        adUser.setPassword(BCrypt.hashpw(adUser.getPassword(),BCrypt.gensalt()));
        adUser.setIsDelete(1);
        adUserMapper.insert(adUser);
        return ResponseResult.okResult("注册成功");
    }
}




